package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class DishServiceImp implements DishService {
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;
    @Override
    public PageResult PageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.select(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //判断是否在售
        for (Long id : ids) {
            Dish dish = setmealMapper.getStatus(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //是否与套餐关联
        List<Long> setMeal = setmealMapper.IsSetMeal(ids);
        if (setMeal!=null && !setMeal.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        for (Long id : ids) {
            setmealMapper.deleteDish(id);
        }
        //删除菜品相关口味
        for (Long id : ids) {
            setmealMapper.deleteFlavor(id);
        }
    }

    @Override
    public DishVO GetDishById(Long id) {
        log.info("根据id查询菜品");
        Dish dish = dishMapper.GetDishById(id);
        DishVO dishVO = new DishVO();
        List<DishFlavor> dishFlavorList = dishFlavorMapper.select(id);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        System.out.println(dishVO);
        return dishVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void UpdateDish(DishDTO dishDTO) {
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.Update(dish);
        dishMapper.deleteByid(dishDTO.getId());
        if (flavors==null|| flavors.isEmpty()){
            return;
        }
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDTO.getId());
            dishFlavorMapper.insert(flavor);
        }
    }
    @Override
    public void IsSale(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.Update(dish);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void DishAdd(DishDTO dishDTO) {
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long id = dishMapper.selectId(dish.getName());
        if (flavors==null||flavors.isEmpty()){
            return;
        }
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
            dishFlavorMapper.insert(flavor);
        }
    }

    @Override
    public List<Dish> GetDishByCategoryId(Long categoryId) {
        return dishMapper.selectBycategoryId(categoryId);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.select(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
